package com.pug.product.service.impl;

import com.pug.product.mapper.InventoryMapper;
import com.pug.product.po.InventoryPO;
import com.pug.product.service.IInventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @program: xq_pug_alibabacloud_parent
 * @description:
 * @author: Mr.Ren
 * @create: 2023-07-02 21:40
 **/
@Service
@Slf4j
public class IInventoryServiceImpl implements IInventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
//    @Transactional(rollbackFor = Exception.class)
//    @GlobalTransactional(rollbackFor = Exception.class)
    public int dedInventory(String productCode, Integer count) {

        InventoryPO inventoryPO = inventoryMapper.selectInventoryByProductCode(productCode);

        int i = inventoryMapper.updateInventoryByProductCode(productCode, inventoryPO.getQuantity() - count);
        if (Objects.equals(productCode,"SZW873")){
            log.error("扣减库存发生异常，数据入库回滚");
            throw new RuntimeException();
        }
        if (i > 0){
            log.info("商品：{}，扣减库存成功，原库存数量：{}，扣除数量：{}",productCode,inventoryPO.getQuantity(),count);
        }else{
            log.info("商品：{}，扣减库存失败");
        }
        return i;
    }
}
